<template>
	<view>
		<view class="uni-padding-wrap uni-common-mt">
			<view class="uni-hello-addfile" @click="chooseImage">+ 请选择文件</view>
		</view>
	</view>
</template>

<script>
	import fileservice from '../../myservice/fileservice.js'

	export default {
		data() {
			return {
				tempFilePaths: [],
				fileIds: []
			}
		},
		props: {
			count: {
				type: Number,
				default: 1,
				//required: false,
			},
		},
		methods: {
			chooseImage() {
				var that = this;
				uni.chooseImage({
					count: that.count,
					success: (res) => {
						//that.tempFilePaths = res.tempFilePaths;
						console.log(JSON.stringify(res))
						//that.imageList = this.imageList.concat(res.tempFilePaths);
						//上传图片到服务器
						res.tempFilePaths.map((value, index) => {
							fileservice.uploadImage(value).then(res => {
								console.log(JSON.stringify(res))
								if (res.statusCode == 200) {
									var data = JSON.parse(res.data);
									that.fileIds.push(data.Id)
									var da = {
										"id": data.Id,
										"src": value
									};
									that.tempFilePaths.push(da);
								} else {
									uni.showToast({
										title: res.errMsg,
										icon: 'none'
									});
								}
							})
						})
					},
					fail: (err) => {
						// #ifdef MP
						uni.getSetting({
							success: (res) => {
								let authStatus = res.authSetting['scope.album'] && res.authSetting[
									'scope.camera'];
								if (!authStatus) {
									uni.showModal({
										title: '授权失败',
										content: 'Hello uni-app需要从您的相机或相册获取图片，请在设置界面打开相关权限',
										success: (res) => {
											if (res.confirm) {
												uni.openSetting()
											}
										}
									})
								}
							}
						})
						// #endif
					}
				});
			},
			getFileList() {
				if (this.fileIds.length < 1) {
					return "";
				} else {
					if (this.count == 1) {
						return this.fileIds[0];
					} else {
						return this.fileIds;
					}
				}
			}
		}
	}
</script>

<style>
</style>
